<?php
class ModelAccountCustomer extends Model {
	public function addCustomer($data) {
		$this->db->query("INSERT INTO `" . DB_PREFIX . "user` SET username = '" . $this->db->escape($data['email']) . "', password = '" . $this->db->escape(md5($data['password'])) . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) ."', company_name = '" . $this->db->escape($data['company_name'])
		. "', opf = '" . $this->db->escape($data['opf']). "', okpo = '" . $this->db->escape($data['okpo']) . "', okonx = '" . $this->db->escape($data['okonx']) . "', inn = '" . $this->db->escape($data['inn']) . "', user_group_id = 11, status = 0, date_added = NOW()");

		$user_id = $this->db->getLastId();
		$this->session->data['customer_id_tmp'] = $user_id;
		if (!isset($data['image'])) {
			$data['image'] = "";
		}
		if (!isset($data['image_l'])) {
			$data['image_l'] = "";
		}
		$this->db->query("INSERT INTO " . DB_PREFIX . "address SET certification = '" . $this->db->escape($data['image']) . "', licenses = '" . $this->db->escape($data['image_l']) . "', user_id = '" . (int)$user_id . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', address_1 = '" . $this->db->escape($data['address_1']) . "', address_2 = '" . $this->db->escape($data['address_2']) . "', city = '" . $this->db->escape($data['city']) . "', postcode = '" . $this->db->escape($data['postcode']) . "', country_id = '" . (int)$data['country_id'] . "', zone_id = '" . (int)$data['zone_id'] . "'");

		$address_id = $this->db->getLastId();

		$this->db->query("UPDATE " . DB_PREFIX . "user SET address_id = '" . (int)$address_id . "' WHERE user_id = '" . (int)$user_id . "'");
	}

	public function updateContractCustomer($contractValue) {
		$this->db->query("UPDATE " . DB_PREFIX . "user SET contract = '" . (int)$this->db->escape($contractValue) . "' WHERE user_id = '" . (int)$this->session->data['customer_id_tmp'] . "'");
		isset($this->session->data['customer_id_tmp']);
	}

	public function editCustomer($data) {
		$this->db->query("UPDATE " . DB_PREFIX . "user SET firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "' WHERE user_id = '" . (int)$this->customer->getId() . "'");
	}

	public function editPassword($email, $password) {
		$this->db->query("UPDATE " . DB_PREFIX . "user SET password = '" . $this->db->escape(md5($password)) . "' WHERE email = '" . $this->db->escape($email) . "'");
	}

	public function editNewsletter($newsletter) {
		$this->db->query("UPDATE " . DB_PREFIX . "user SET newsletter = '" . (int)$newsletter . "' WHERE user_id = '" . (int)$this->customer->getId() . "'");
	}

	public function getCustomer($user_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user WHERE user_id = '" . (int)$user_id . "'");

		return $query->row;
	}

	public function getTotalCustomersByEmail($email) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "user WHERE email = '" . $this->db->escape($email) . "'");

		return $query->row['total'];
	}
}
?>